package com.xiaoq.oauth2.thirdpartclient.controller;


import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xiaoq.oauth2.thirdpartclient.entity.Client;
import com.xiaoq.oauth2.thirdpartclient.service.ClientService;

import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/client")
public class ClientController {

    private Logger logger = LoggerFactory.getLogger(ClientController.class);

    @Autowired
    private ClientService clientService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response) {
        return null;
    }

    @RequestMapping(value = "/clients", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.GET)
    @ApiOperation(value = "分页方式取得第三方注册应用接口",notes = "分页方式取得第三方注册应用接口")
    public Object clients(@ApiParam(required = true, name = "page", value = "当前页码")@RequestParam("page") String page,
                                           @ApiParam(required = true, name = "limit", value = "每页显示数据条数")@RequestParam("limit") String limit){

        return null;
    }


    /*保存数据对象 */
    @RequestMapping(value="/save", method = RequestMethod.GET)
    public String save(Model model, Client client){
        clientService.createClient(client);
        return "redirect:/client";
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String showCreateForm(Model model) {
        model.addAttribute("client", new Client());
        model.addAttribute("op", "新增");
        //转向editclient.ftl
        return "editclient";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(Client client, RedirectAttributes redirectAttributes) {
        clientService.createClient(client);
        redirectAttributes.addFlashAttribute("msg", "新增成功");
        return "redirect:/client";
    }

    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public String showUpdateForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("client", clientService.findById(id));
        model.addAttribute("op", "修改");
        //转向editclient.ftl
        return "editclient";
    }

    @RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
    public String update(Client client, RedirectAttributes redirectAttributes) {
        clientService.updateClient(client);
        redirectAttributes.addFlashAttribute("msg", "修改成功");
        return "redirect:/client";
    }

    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String showDeleteForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("client", clientService.findById(id));
        model.addAttribute("op", "删除");
        //转向editclient.ftl
        return "editclient";
    }

    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        clientService.deleteClient(id);
        redirectAttributes.addFlashAttribute("msg", "删除成功");
        return "redirect:/client";
    }

}
